<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
	</head>
	<body>
		<script type="text/javascript">
			//对角线元素，要么横坐标等于纵坐标，要么横坐标+纵坐标的和为矩阵的行数-1
			var diagonalSum = function(mat) {
				const n = mat.length;
				let sum = 0;
				for (let i = 0; i < n; i++) {
					for (let j = 0; j < n; j++) {
						if (i == j || i + j == n - 1) {
							sum += mat[i][j];
						}
					}
				}
				return sum;
			};
			console.log(diagonalSum([
				[1, 2, 3],
				[4, 5, 6],
				[7, 8, 9]
			]));
		</script>
	</body>
</html>
